package service

import (
	"fmt"
	ClickHouse "gin/database"
	"gin/model"
	"strconv"
	"strings"
)

type Status struct {
}

// GetStatusIdsByCorrespond 通过 corresponds 获取状态 ids
func (c *Status) GetStatusIdsByCorrespond(corresponds []string) (ipStatusIds []string, statusMap map[int64]string) {

	statusIds := make([]model.Status, 0)
	statusDb := ClickHouse.DB.Model(&model.Status{})
	statusDb.Where("correspond IN ?", corresponds)
	statusDb.Find(&statusIds)

	var ipStatusMap = make(map[int64]string)
	for _, statusItem := range statusIds {
		ipStatusIds = append(ipStatusIds, strconv.FormatInt(statusItem.Id, 10))
		ipStatusMap[statusItem.Id] = statusItem.Name
	}

	return ipStatusIds, ipStatusMap
}

// GetStatusIdsByCode 通过 Code 获取状态 ids
func (c *Status) GetStatusIdsByCode(corresponds []string) (ipStatusIds []string, statusMap map[int64]string) {

	statusIds := make([]model.Status, 0)
	statusDb := ClickHouse.DB.Model(&model.Status{})
	statusDb.Where("code IN ?", corresponds)
	statusDb.Find(&statusIds)

	var ipStatusMap = make(map[int64]string)
	for _, statusItem := range statusIds {
		ipStatusIds = append(ipStatusIds, strconv.FormatInt(statusItem.Id, 10))
		ipStatusMap[statusItem.Id] = statusItem.Name
	}

	return ipStatusIds, ipStatusMap
}

// GetStatusIdGroupByCorrespond 获得状态范围所有状态id
func (c *Status) GetStatusIdGroupByCorrespond() (res map[string][]string, err error) {
	var statusList []model.Status
	dbr := ClickHouse.DB.Model(&model.Status{}).Find(&statusList)
	if dbr.Error != nil {
		err = dbr.Error
		return
	}
	res = make(map[string][]string)
	for _, status := range statusList {
		res[status.Correspond] = append(res[status.Correspond], fmt.Sprintf("%d", status.Id))
	}
	return
}

// GetStatus 查询状态列表
func (c *Status) GetStatus() (res []model.Status, err error) {
	err = ClickHouse.DB.Model(&model.Status{}).Find(&res).Error
	return
}

//GetNotStartedStatus 获取未完成的状态
func (c *Status) GetNotStartedStatus() (res []model.Status, err error) {
	options := model.Options{}
	config, errs := options.GetOptionsConfigMultiItemData("default_task_status", []string{"default_status", "default_ready_status", "default_wait_assemble_status"})
	if errs != nil {
		err = errs
		return
	}

	var configValue []string
	for _, configItem := range config {
		configValue = append(configValue, fmt.Sprintf("'%s'", configItem))
	}

	if len(configValue) > 0 {
		configString := strings.Join(configValue, ",")
		res, err = c.GetList(fmt.Sprintf("code in (%s)", configString), "*")
	}
	return
}

//GetList 获得多条状态
func (c *Status) GetList(filter string, field string) (res []model.Status, err error) {
	statusModel := ClickHouse.DB.Model(&model.Status{})

	if filter != "" {
		statusModel = statusModel.Where(filter)
	}
	if field != "" {
		statusModel = statusModel.Select(field)
	}

	err = statusModel.Find(&res).Error
	return
}

//GetStatusCodeMap 获取映射
func (c *Status) GetStatusCodeMap(param map[string]string) (result map[string]model.Status, err error) {
	res, err := c.GetList(param["filter"], param["field"])

	if err != nil {
		return
	}

	result = map[string]model.Status{}
	for _, resItem := range res {
		result[resItem.Code] = resItem
	}

	return
}

// getStatusById 获得状态id map
func (c *Status) getStatusById() (statusMap map[int64]*model.Status, err error) {
	statusMap = make(map[int64]*model.Status)
	var statusList []model.Status
	err = ClickHouse.DB.Model(&model.Status{}).Find(&statusList).Error
	for i, v := range statusList {
		statusMap[v.Id] = &statusList[i]
	}
	return
}
